home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: C constant expression declarations
- Date: Sat, 17 Feb 96 22:23:12 GMT
- Organization: none
- Message-ID: <824595792snz@genesis.demon.co.uk>
- References: <31229735.41C67EA6@isi.com> <4fvl5cINN94q@keats.ugrad.cs.ubc.ca> <4g2nha$ksa@sun001.spd.dsccc.com> <4g3fkeINNoj7@keats.ugrad.cs.ubc.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4g3fkeINNoj7@keats.ugrad.cs.ubc.ca>
- c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
-
- >Parentheses are intended to override operator precedence, and to occasionally
- >make a complex expression clearer to someone reading them; a proverbial icing
- >on the cake of sorts.
- >
- >if (x == 3 || y > 4 && z < 5)
- >
- >is easier to read than
- >
- >if ((x == 3) || ((y > 4) && (z < 5)))
-
- However these examples have little relevance to the case in question which is
- macro definitions of expressions. Here enclosing parentheses are essential
- to ensure correct grouping after substitution within a more general
- expression. The only exception is when the replacement text consists of
- a single token e.g.
-
- #define VAL1 1
-
- Now the question is whether this should be treated as a special case and
- not written with parentheses, or written with parentheses to maintain a
- consistent style and reduce the chance of mistakes such as:
-
- #define VALX -1
-
- Expression complexity is not an issue here since the case where this is an
- issue is by its very nature the simplest possible one. Since negative
- constants must be written with parentheses it is natural for positive
- constants to be written in a consistent way.
-
- #define RESULT_A (0)
- #define RESULT_B (-1)
-
- is easier or at least more consistent and more pleasant to read than:
-
- #define RESULT_A 0
- #define RESULT_B (-1)
-
- (generalise to larger lists of codes).
-
- >I put my extra discipline into consciously knowing what I'm doing. I _am_
- >awake; I _did_ drink my coffee. I will not write things that don't _buy_
- >anything. They obfuscate programs more than they help.
-
- Anybody can make mistakes. Using a consistent style means they are much
- easier to spot. Something like this is likely to be a project-wide
- convention. Do you trust other people as much as you trust yourself?
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-